// JavaScript Document

/* -----------------------------------------------
   Floating layer - v.1
   (c) 2006 www.haan.net
   contact: jeroen@haan.net
   You may use this script but please leave the credits on top intact.
   Please inform us of any improvements made.
   When usefull we will add your credits.
  ------------------------------------------------ */

x = 20;
y = 70;
function setVisible(obj,doAction,inputcom,divcom,chkcom)
{

			var objChk = chkcom;
			
	if (doAction==2){
//		alert('i am in');

		var c_value = "";
		var c_name = "";
		

//		alert(objChk.length);
		for (var i=0; i < objChk.length; i++) {
		   if (objChk[i].checked){
		   	   checkBoxVal = objChk[i].value;
			   
		   	   temp = checkBoxVal.split('-');
		       c_value += "," + temp[0] ;
			   c_name  += ", " + temp[1] ;
			   
      		}
   		}
		c_value = c_value.substring(1,c_value.length);
		c_name = c_name.substring(1,c_name.length);
		document.getElementById(inputcom).value=c_value;
		document.getElementById(divcom).innerHTML = c_name;
		//alert(document.getElementById(inputcom).value);
		//alert(document.getElementById(divcom).innerHTML);
	}else{
	
		tagids = document.getElementById(inputcom).value;
		tagids = tagids.split(",");
		//k=0;
		if (tagids.length>0){
		 for (var k = 0; k < tagids.length; k++){
			for (var i=0; i < objChk.length; i++) {
				
				checkBoxVal = objChk[i].value;
				
				 temp = checkBoxVal.split('-');
			   if (tagids[k]==temp[0]){
				   //alert(checkBoxVal);
			   		objChk[i].checked=true;
					//k = k+1;
				}
			}
		  }
			
		}		
		
	}
	placeIt(obj);
	obj = document.getElementById(obj);
	obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
	
}


function openSitePopUp(obj,doAction,inputcom,divcom,chkcom)
{

	var objChk = chkcom;
			
	if (doAction==2){
	
		var mainDiv = document.getElementById('sites');
		
		var currChild = mainDiv.childNodes;
		var childCount = currChild.length;

		for (var k=0;k<childCount;k++) {
			var objchild = currChild[k];
			//mainDiv.removeChild(currChild[0]);
			if(objchild.id){
			objchild.style.display='none';
			//alert(objchild.id);
			}
		}
	
		
		var c_value = "";
		var c_name = "";
		
		for (var i=0; i < objChk.length; i++) {
		   if (objChk[i].checked){
		   	   checkBoxVal = objChk[i].value;
			   
		   	   temp = checkBoxVal.split('-');
		       c_value += "," + temp[0] ;
			   c_name  += ", " + temp[1] ;
			   addSite(temp[1],temp[0] );
			   
      		}
   		}
		 addSite('EMPTY',0 );
		 
		c_value = c_value.substring(1,c_value.length);
		c_name = c_name.substring(1,c_name.length);
		document.getElementById(inputcom).value=c_value;
		document.getElementById(divcom).innerHTML = c_name;
	}else{
		tagids = document.getElementById(inputcom).value;
		tagids = tagids.split(",");
		k=0;
		if (tagids.length>0){
			
			for (var i=0; i < objChk.length; i++) {
				
				checkBoxVal = objChk[i].value;
				
				 temp = checkBoxVal.split('-');
			   if (tagids[k]==temp[0]){
				   //alert(checkBoxVal);
			   		objChk[i].checked=true;
					k = k+1;
				}
			}
		}
	}
	placeIt(obj);
	obj = document.getElementById(obj);
	obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
	
}

function placeIt(obj)
{
	//alert(obj);
	objName = obj;
	obj = document.getElementById(obj);
	if (document.documentElement)
	{
		theLeft = document.documentElement.scrollLeft;
		theTop = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		theLeft = document.body.scrollLeft;
		theTop = document.body.scrollTop;
	}
	theLeft += x;
	theTop += y;

	obj.style.left = 500 + 'px' ;
	obj.style.top = 200 + 'px' ;
	setTimeout("placeIt('"+objName+"')",500);
}
//window.onscroll = setTimeout("placeIt('categorylayer')",500);
//window.onscroll = setTimeout("placeIt('regionlayer')",500);


function ChannelPopUpShowHide(idname, imgid)
{
	var tar_obj = document.getElementById(idname);
	var img_obj = document.getElementById(imgid);
	
	if (tar_obj.style.display == 'none')
	{
		tar_obj.style.display = 'block';
//		img_obj.src = 'images/kanulu_arrow_down.jpg';
	}else{
		tar_obj.style.display = 'none';
	//	img_obj.src = 'images/kanulu_arrow_up.jpg';
	}
	
	
	theLeft = img_obj.offsetLeft;
	theTop = img_obj.offsetTop;

	tar_obj.style.left = (theLeft)+'px';
	tar_obj.style.top = theTop+20 +'px';

}


var IE = false ;
if (window.navigator.appName.indexOf("Explorer") !=-1)
{
	IE = true;
}


function resizeFrame(name, minHeight)
{
	if(!minHeight) minHeight = '600';

	if(!name || name=="") return;

	try
	{
		if (IE)
			var oBody = document.frames(name).document.body;
		else
			var oBody = document.getElementById(name).contentDocument.body;
		
		var oIFrame = document.getElementById(name);
		oIFrame.height = minHeight;
		var frmWidth  = oBody.scrollWidth;
		var frmHeight = oBody.scrollHeight;
		
		//alert(oBody.scrollHeight); 
		
		if (frmHeight > minHeight){
		
			//if (window.navigator.appName == 'Netscape' && frmHeight > 32000)
			//	oIFrame.height= 32000;
			//else
				oIFrame.height= frmHeight;				
			
		}
		else
			oIFrame.height = minHeight;
	}catch (e) {}
	
	
}


function setChannel(chnID){
	var qurystr = document.URL
	
	temp = qurystr.split('?');

	if (temp[1]){
		document.location.href='?'+ temp[1] + '&chn='+ chnID;
	}else{
		document.location.href='?chn=' + chnID;
	}
}


function setSite(site,siteid){
		document.location.href='/sites/'+site+'/?siteid='+siteid;
}





function openChannelPopUp(sid,addType,toDiv,toCom,hrefID, userid){

	//alert('Channel Site id ' + addType + ' LINK ' + hrefID+sid);

	var popup_obj = document.getElementById('sites_channellayer');
	var link_obj = document.getElementById(hrefID+sid);
	
	if (popup_obj.style.visibility == 'visible')
	{
		popup_obj.style.visibility = 'hidden';
//		img_obj.src = 'images/kanulu_arrow_down.jpg';
	}else{
		popup_obj.style.visibility = 'visible';
	//	img_obj.src = 'images/kanulu_arrow_up.jpg';
	}
	
	
	theLeft = link_obj.offsetLeft;
	theTop = link_obj.offsetTop;
	//alert(theLeft + ' -- ' + theTop + link_obj.id);
	popup_obj.style.left = (theLeft)+300+'px';
	popup_obj.style.width = '650px';
	popup_obj.style.top = theTop+450 +'px';
	//alert('http://www.xprsdata.com/sites/admin/admin/inc/site_channel_module_popup.php?siteid='+sid+'&addtype='+addType+'&todiv='+toDiv+'&tocom='+toCom+'&userid='+userid);
	doRequest('http://www.xprsdata.com/sites/admin/admin/inc/site_channel_module_popup.php?siteid='+sid+'&addtype='+addType+'&todiv='+toDiv+'&tocom='+toCom+'&uid='+userid,'sites_channellayer', toCom);
}


function addSelectedChkBox(sid,popupDIV,toDiv,toCom,chkcom,chkcomCount){

		//alert(sid + ' - '+ addType + ' - '+ toDiv + ' - '+ toCom + ' - '+ chkcom + ' - '+ chkcomCount)
		var c_value = "";
		var c_name = "";

		for (var i=1; i < chkcomCount; i++) {
		   objName=''+chkcom+i;
		   
		   var objChk = document.getElementById(objName);
		   	//alert(objChk.value);
		   
		   if (objChk.checked){
		   	   checkBoxVal = objChk.value;
			   
		   	   temp = checkBoxVal.split('-');
		       c_value += "," + temp[0] ;
			   c_name  += ", " + temp[1] ;
			 // alert(temp[0]);
      		}
			 
   		}
		//alert( c_value + ' -- ' + c_name );
		c_value = c_value.substring(1,c_value.length);
		c_name = c_name.substring(1,c_name.length);
		//alert(toCom);
		document.getElementById(toCom).value=c_value;

		document.getElementById(toDiv).innerHTML = c_name;
		//alert(document.getElementById(toDiv).innerHTML + ' - ' + toDiv);


	var popup_obj = document.getElementById(popupDIV);
	
	if (popup_obj.style.visibility == 'visible')
	{
		popup_obj.style.visibility = 'hidden';
//		img_obj.src = 'images/kanulu_arrow_down.jpg';
	}else{
		popup_obj.style.visibility = 'visible';
	//	img_obj.src = 'images/kanulu_arrow_up.jpg';
	}
	
	
	

}




function openModulePopUp(sid, hrefID){
		alert('Module Site id' + sid + ' LINK ' + hrefID+sid);
}




function addSite(sitetitle,siteid) {
	//alert('Hello');
	PARENT_DIV_NAME = "sites";
	CHILD_DIV_NAME = "site_selection_div";
	

	
	var  counter;
	counter++;
	var strdivName=CHILD_DIV_NAME+siteid;
	//alert(CHILD_DIV_NAME+siteid);
	var divExisit = document.getElementById(CHILD_DIV_NAME+siteid);
	if (divExisit && siteid ){
		//alert('DIV Exisits');
		divExisit.style.display='block';
	}else{
		document.getElementById('site_selection_title').innerHTML= sitetitle;
		//document.getElementById('site_channels_').id ='site_channels_'+siteid;
		//currFileName();
		//document.getElementById("Step3TotFiles").innerHTML = counter+" Files";
		document.getElementById("site_modules").innerHTML = '<a id="mod_popup_'+siteid+'" href="javascript: openChannelPopUp('+ siteid +',\'module\',\'site_modules_'+siteid+'\',\'site_modules_ids_'+siteid+'\',\'mod_popup_\');" >Select Modules</a><div id="site_modules_'+siteid+'"></div><input type="hidden" name="site_modules_ids_'+siteid+'" id="site_modules_ids_'+siteid+'" value="" />';
		
		document.getElementById("site_channels").innerHTML = '<a id="chn_popup_'+siteid+'" href="javascript: openChannelPopUp('+ siteid +',\'channel\',\'site_channels_'+siteid+'\',\'site_channels_ids_'+siteid+'\',\'chn_popup_\');" >Select Channels</a><div id="site_channels_'+siteid+'"></div><input type="hidden" name="site_channels_ids_'+siteid+'" id="site_channels_ids_'+siteid+'" value="" />';
	
		var newFields = document.getElementById(CHILD_DIV_NAME).cloneNode(true);
		//alert(newFields.id);
		newFields.id = strdivName;
		if(siteid){
			newFields.style.display = 'block';
		}
		var newField = newFields.childNodes;
	
		for (var i=0;i<newField.length;i++) {
			var theID = newField[i].id
			//if (theID == 'site_channels_'){
			//	newField[i].id = theID+siteid;
			//	alert(newField[i].id);
			//}
				//alert(newField[i].id);
		}
		var insertHere = document.getElementById(PARENT_DIV_NAME);

		//insertHere.parentNode.insertBefore(newFields,insertHere);
		insertHere.appendChild(newFields);
	}
}








function showEmailContent(mid,inbox,hrefID,popup, toDiv){

	//alert('Channel Site id ' + addType + ' LINK ' + hrefID+sid);

	var popup_obj = document.getElementById(popup);
	var link_obj = document.getElementById(hrefID+mid);
	
	if (popup_obj.style.visibility == 'visible')
	{
		popup_obj.style.visibility = 'hidden';
//		img_obj.src = 'images/kanulu_arrow_down.jpg';
	}else{
		popup_obj.style.visibility = 'visible';
	//	img_obj.src = 'images/kanulu_arrow_up.jpg';
	}
	document.getElementById('content').value = mid;
	if(link_obj){
		theLeft = link_obj.offsetLeft;
		theTop = link_obj.offsetTop;
		//alert(theLeft + ' -- ' + theTop + link_obj.id);
		popup_obj.style.left = (theLeft)+200+'px';
		popup_obj.style.top = theTop+450 +'px';
		//alert('http://www.xprsdata.com/sites/editor/inbox2/emailcontent.php?id='+mid+'&inbox='+inbox);
		makeRequest('http://www.xprsdata.com/sites/editor/inbox/emailcontent.php?id='+mid+'&inbox='+inbox,toDiv);
	}
}




function isValidURL(url){ 
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 

	var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
	
	if (urlRegxp.test(url) != true) {
		
		return false; 
		
	}else{
		
		if(RegExp.test(url)){ 
		
			return true; 
		}else{ 
			//alert("Not a valid URL, please input correct URL.")
			return false; 
		} 
	}
}


