// This vars must be altered
var productionSecure = "https://www.johnsonsbaby.com/";
var productionInSecure = "http://www.johnsonsbaby.com/";
var stageSecure = "http://148.177.2.31:7120/";
var stageInSecure = "http://148.177.2.31:8120/";
var stagePort = ":8120";       //example=  :XXXX
var stagePortSecure = ":7120"; //example=  :XXXX




function isUndefined(a) {
   	return typeof a == 'undefined';
} 	

function getAddress(pageUrl_){
		var address = window.location.href;
		var port;
		address = address.substring(7);
		
		if (address.indexOf(':') == -1 ) {
			address = productionSecure + pageUrl_; 
		} else {
		
		// begin - configuration to stalin and localhost
		// doesn't need alter
			port = address.substring(10,14);
			if	(address.indexOf('localhost') == 0 ){
				return 	address = 'http://localhost:'+port+'/' + pageUrl_;
			}else if (address.indexOf('stalin') == 0 ){
				if(address.indexOf('stalin.cit') == 0 ){
					port = address.substring(10,15);
					return 	address = 'http://stalin.cit:'+port+'/' + pageUrl_;
				}else{
					port = address.substring(7,11);
					return 	address = 'http://stalin:'+port+'/' + pageUrl_;
				}
			}
		// end - configuration to stalin and localhost
		
		address = stageSecure + pageUrl_; 
		
		}
		return address;
}

/* 
This function is used to create secure links
parameters:
	1.pageUrl_ 	= page name that is used to create the link
	2.pagename 	= content that will be showed in the link (it can be an image to eg. <img..../> or a simple text)
	3.style    	= style of element
	4.event_   	= event
	5.secure_id	= any string 
	
NOTE: if this function is used more than one time in the same page the parameter 'secure_id' must be different.
 */	
function ssiForm(pageUrl_,pagename,style,title,event_,secure_id)
	{

		var address = getAddress(pageUrl_);
		
		if(isUndefined(secure_id)){
			secure_id = '';
		}

		link_final = '<a id ="secure_'+secure_id+'"';
		if(!isUndefined(style)){
			link_final = link_final + ' class="' + style + '" ';
		}
		if(!isUndefined(title)){
			link_final = link_final + ' title="' + title + '" ';
		}
		if(!isUndefined(event_)){
 			link_final = link_final + ' ' + event_ + ' ';
		}
		link_final = link_final + ' href="' + address + '">' + pagename + '</a>';

		return link_final;
	}
	
/* 
This function is used to redirect page
parameters:
	1.pageUrl_ 	= page name that is used to redirect
	2.secure 	= values true or false to indicate 

 */	
function ssiRedirect(pageUrl_,secure)
	{
	var productionUrl;
	var stageUrl;
	if (secure){
		productionUrl = productionSecure;
	    stageUrl = stageSecure;
	}else{
	 	productionUrl = productionInSecure;
	 	stageUrl = stageInSecure;	
	}
	
		var address = window.location.href;
		var port;
		address = address.substring(7);
		if (address.indexOf(':') == -1 ) {
			address = productionUrl + pageUrl_; 
		} else {

			port = address.substring(10,14);
			if	(address.indexOf('localhost') == 0 ){
			
				alert('localhost- '+port+ ' - http://localhost:'+port+'/' + pageUrl_ )
				return 'http://localhost:'+port+'/' + pageUrl_;
			}else if (address.indexOf('stalin') == 0 ){
				if(address.indexOf('stalin.cit') == 0 ){
					port = address.substring(10,15);
					return 'http://stalin.cit:'+port+'/' + pageUrl_;
				}else{
					port = address.substring(7,11);
					return 'http://stalin:'+port+'/' + pageUrl_;
				}
	
			}
		
		return  stageUrl + pageUrl_; 
		
		}

	}		

/* 
This function is used to modify all url from secure links(https) to unsecure links(http)
All links that id contain the word 'secure' (eg. secure, secure_1, secure1, securelink, linksecure)
it won't be altered because should continue being secure

note: dontīt forget...ALL THE PORTS MUST BE ALTERED TO WORK ON STAGE! 
 */	
function setLinksUnSecure(){
	
	var address = getAddress('');
	
	for(i = 0; i < document.getElementsByTagName('a').length; i++ ){
		var element = document.getElementsByTagName('a')[i];
		var url = element.href;
		var newUrl;

		if (address.indexOf(':') == -1 ) {
			// on production
			// if have 'https' and url id contain 'secure'
			if ((url.indexOf('https:') != -1) && (element.id.indexOf('secure') != 0) ) {
				newUrl = new String (url.replace(/https:/,'http:'));
				element.href = newUrl;
			}
			
		} else if (address.indexOf(stagePortSecure) != -1 ) {
			//if is stage and secure url
			if (url.indexOf(stagePortSecure) != -1 && element.id.indexOf('secure') != 0 ) {
				var re = new RegExp(stagePortSecure,'gim');
				newUrl = new String (url.replace(re,stagePort));
				element.href = newUrl;	
			}	
			
		}
	}
}	



function setUrlNotSecure(url){
	
	var address = url;
	
		if (address.indexOf(':') == -1 ) {
			// on production
			// if have 'https' and url id contain 'secure'
			if ((url.indexOf('https:') != -1) && (element.id.indexOf('secure') != 0) ) {
				newUrl = new String (url.replace(/https:/,'http:'));
				return newUrl;
			}
			
		} else if (address.indexOf(stagePortSecure) != -1 ) {
			//if is stage and secure url
				var re = new RegExp(stagePortSecure,'gim');
				newUrl = new String (url.replace(re,stagePort));
				return newUrl;	
		}
	return url;
}


/* 
This function is used to create option at the comboBox
 */
function getOptions(pagename,optionName){

	var address = getAddress('');
	var option;


	return option = '<option selected value="' + address + pagename + '"> '+optionName+'</option>';

}

function getOptionsNotSecure(pagename,optionName){

	var address = setUrlNotSecure(getAddress(''));
	var option;


	return option = '<option value="' + address + pagename + '"> '+optionName+'</option>';

}


