// JavaScript Document

/***************************************************************************************************
This function is the one hides/shows the extra fields in the admin
*****************************************************************************************************/
function togglediv(targetId){
	target= document.getElementById(targetId);
	if (target.style.display == "none") { target.style.display = "block"; }
	else { target.style.display = "none"; }
}

function toggledivs(showid,hideid){
	show = document.getElementById(showid);
	hide = document.getElementById(hideid);
	
	hide.style.display = "none";
	show.style.display = "block";
}

/***************************************************************************************************
This function hides/shows nav sections in the admin according to the cookie that has been set
*****************************************************************************************************/
function navdisplay(cookieName) {
	var theCookie = ""+document.cookie;
	var ind = theCookie.indexOf(cookieName);
	if (ind == -1 || cookieName=="") return ""; 
	var ind1 = theCookie.indexOf(';',ind);
	if (ind1 == -1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

/***************************************************************************************************
This function hides/shows nav sections in the admin and sets a cookie to record it
*****************************************************************************************************/
function togglenav(targetId, action){
	target = document.getElementById(targetId);
	button = "bt" + targetId;
	buttonsrc = document.getElementById(button);
	
	var theDate = new Date();
	var oneYearLater = new Date( theDate.getTime() + 31536000000 );
	var expiryDate = oneYearLater.toGMTString();
	
	if(action == "show"){
		target.style.display = "block";
		buttonsrc.src = "/img/admin/bt_minus.gif";
		document.cookie = targetId + "=show; expires=" + expiryDate + "; path=/";
	} else if(action == "hide"){
		target.style.display = "none";
		buttonsrc.src = "/img/admin/bt_plus.gif";
		document.cookie = targetId + "=hide; expires=" + expiryDate + "; path=/";
	} else {
		if (target.style.display == "none") {
			target.style.display = "block";
			buttonsrc.src = "/img/admin/bt_minus.gif";
			document.cookie = targetId + "=show; expires=" + expiryDate + "; path=/";
		} else {
			target.style.display = "none";
			buttonsrc.src = "/img/admin/bt_plus.gif";
			document.cookie = targetId + "=hide; expires=" + expiryDate + "; path=/";
		}
	}
}




function delcat_submit(delcatid) {
    // confirmation message
    var message = "";
    var submitOK=true;
    
    delcatip=document.getElementById('delcatid');
    delcatip.value=delcatid;
    

    var theform = document.mainform;
    theform.submit();

}



/*********************************************************************************************************
to verify input before submission of site data
***********************************************************************************************************/

function sitesubmitcheck() {
    // confirmation message
    var message = "";
    var submitOK=true;
    
    jumptextbox=document.getElementById('jumpurl');
    jumptext=jumptextbox.value;
    displaytextbox=document.getElementById('displayurl');
    displaytext=displaytextbox.value;
    reciprocaltextbox=document.getElementById('reciprocalurl');
    reciprocaltext=reciprocaltextbox.value;
    
    
    if((jumptext!="http://") && (jumptext!="")){
        if(!jumptext.match('^http(s){0,1}\:\/\/')){
            submitOK=false;
            message="JumpURL must be prepended by either http:// or https://";
        }
        if(!(ValidateLink(jumptext))){
            submitOK=false;
            message="JumpURL must be a valid URL";
        }
    }
    if((displaytext!="http://") &&  (displaytext!="")){
        if(!displaytext.match('^http(s){0,1}\:\/\/')){
            submitOK=false;
            message="DisplayURL must be prepended by either http:// or https://";
        }
        if(!(ValidateLink(displaytext))) {
            submitOK=false;
            message="DisplayURL must be a valid URL";
        }
        
    }
    
    if((reciprocaltext!="http://") &&  (reciprocaltext!="")) {
        if(!reciprocaltext.match('^http(s){0,1}\:\/\/')){
            submitOK=false;
            message="ReciprocalURL must be prepended by either http:// or https://";
        }
        
        if((reciprocaltext!="http://") &&  (!(ValidateLink(reciprocaltext)))){
            submitOK=false;
            message="ReciprocalURL must be a valid URL";
        }
    }
    
    assoctextbox=document.getElementById('new2ndURL');
    if(assoctextbox){ // only if it exists
        assoctext=assoctextbox.value;
        if((assoctext!="http://") &&  (assoctext!="")) {
            if(!assoctext.match('^http(s){0,1}\:\/\/')){
                submitOK=false;
                message="Associated URL must be prepended by either http:// or https://";
            }
            if(!(ValidateLink(assoctext))) {
                submitOK=false;
                message="Associated URL must be a valid URL";
            }
        }
    }
    
    
    
    if (submitOK) {
            var theform = document.mainform;
            theform.submit();
    } else {
            var return_value = alert(message);
    }
}


function ValidateLink(userLink) 
 { 
    var v = new RegExp(); 
    //v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
    //if (!v.test(userLink.value)) 
    if(!userLink.match("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&+\?\~\/.=,]+$"))
     { 
        //alert("You must supply a valid URL.It should be in following format \n http://www.userurl.com "); 
        return false; 
     } 
     else return true;
 } 




/***************************************************************************************************
This function ask if you really want to delete the url check results that you have selected
*****************************************************************************************************/
function urlcheckDelete() {
	// CONFIRM REQUIRES ONE ARGUMENT
	var message = "Are you sure you want to delete these listings?";
	// CONFIRM IS BOOLEAN. THAT MEANS THAT
	// IT RETURNS TRUE IF 'OK' IS CLICKED
	// OTHERWISE IT RETURN FALSE
	var return_value = confirm(message);
	// TEST TO SEE IF TRUE|FALSE RETURNED
	if (return_value == true) {
		// YOUR 'OK' SCRIPT GOES HERE
		//window.parent.location="/admin/devmarketingsites.php?siteid=" + siteid + "&mode=delete";
		var theform = document.delform;
 		theform.submit();
	} else {
		// YOUR 'CANCEL' SCRIPT GOES HERE
	}
}

/***************************************************************************************************
This function ask if you really want to delete a submitted site
*****************************************************************************************************/
function submissioncheckDelete() {
	// CONFIRM REQUIRES ONE ARGUMENT
	var message = "Are you sure you want to delete these site submissions?";
	// CONFIRM IS BOOLEAN. THAT MEANS THAT
	// IT RETURNS TRUE IF 'OK' IS CLICKED
	// OTHERWISE IT RETURN FALSE
	var return_value = confirm(message);
	// TEST TO SEE IF TRUE|FALSE RETURNED
	if (return_value == true) {
		// YOUR 'OK' SCRIPT GOES HERE
		//window.parent.location="/admin/devmarketingsites.php?siteid=" + siteid + "&mode=delete";
		var theform = document.submissionform;
 		theform.submit();
	} else {
		// YOUR 'CANCEL' SCRIPT GOES HERE
	}
}


/***************************************************************************************************
This function detaches a site listing from a sales contact
*****************************************************************************************************/
function scSiteListingDel(sitelistingfield,sitelistingid){
	// CONFIRM REQUIRES ONE ARGUMENT
	var message = "Are you sure you want to dettach this Site Listing from this Sales Contact?";
	// CONFIRM IS BOOLEAN. THAT MEANS THAT
	// IT RETURNS TRUE IF 'OK' IS CLICKED
	// OTHERWISE IT RETURN FALSE
	var return_value = confirm(message);
	// TEST TO SEE IF TRUE|FALSE RETURNED
	if (return_value == true) {
		// YOUR 'OK' SCRIPT GOES HERE
		target = document.getElementById(sitelistingfield);
		target.value = sitelistingid;
		var theform = document.editform;
 		theform.submit();
	} else {
		// YOUR 'CANCEL' SCRIPT GOES HERE
	}
}


/***************************************************************************************************
This function deletes a sales contact history record from a sales contact
*****************************************************************************************************/
function scHistoryDel(historyfield,historyid){
	// CONFIRM REQUIRES ONE ARGUMENT
	var message = "Are you sure you want to delete this Sales Contact History from this Sales Contact?";
	// CONFIRM IS BOOLEAN. THAT MEANS THAT
	// IT RETURNS TRUE IF 'OK' IS CLICKED
	// OTHERWISE IT RETURN FALSE
	var return_value = confirm(message);
	// TEST TO SEE IF TRUE|FALSE RETURNED
	if (return_value == true) {
		// YOUR 'OK' SCRIPT GOES HERE
		target = document.getElementById(historyfield);
		target.value = historyid;
		var theform = document.editform;
 		theform.submit();
	} else {
		// YOUR 'CANCEL' SCRIPT GOES HERE
	}
}


/***************************************************************************************************
Deletes the selected Article Image
*****************************************************************************************************/
function first15ImageDelete(image){
	var message = "Are you sure you want to delete this Image?"; // Confirm requires one argument
	// Confirm is boolean. That means that it returns TRUE if 'OK' is clicked otherwise it returns FALSE.
	var return_value = confirm(message);
	if (return_value == true) { // Test to see if TRUE/FALSE is returned.
		/* Your 'OK script goes here */
		document.getElementById(image).value = "remove";
		document.first15form.submit();
	} else { /* Your 'CANCEL' script goes here. */ }
}


/***************************************************************************************************
This function shows the specified div on the author signup/login page and hides the others
*****************************************************************************************************/

function authordiv(targetId){
	target = document.getElementById(targetId);
		if(target.style.display == "none") {
			document.getElementById("authorsignup").style.display = "none";
			document.getElementById("authorlogin").style.display = "none";
			target.style.display = "block";
		}
}


/***************************************************************************************************
This function will disable an entire form
*****************************************************************************************************/

function disableForm(form){
  formelems = form.elements;
  for(i=0;i<formelems.length;i++){
    formelems[i].disabled = "True";
  }
}