/* The following function creates an XMLHttpRequest object... */

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/
/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject(); 

/* Function called to get the product categories list */
function getProducts(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will 		
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/

	
	index_value=document.getElementById('brand');
	value_index= index_value.options[index_value.selectedIndex].value;
	http.open('get', 'classes_mj/ajax_handler.php?action=get_keywords&id=' 
			+ value_index+'&selectedstr='+document.getElementById('metaHidden').value);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleProducts; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
function handleProducts(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('keyword_div').innerHTML = response;
		document.getElementById('keyword_div').style.display='block';
		//alert(response);
	}
}

////////////////////////////////////////////////////////////
// Function to fetch all the keywords from the cloud
///////////////////////////////////////////////////////////
function getKeywordCloud() {
		

	http.open('get', 'classes_mj/ajax_handler.php?action=get_keywordcloud'+'&selectedstr='+document.getElementById('metaHidden').value);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = HandleKeywordCloud; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

function HandleKeywordCloud() {
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		document.getElementById('keyword_div').innerHTML = response;
		document.getElementById('keyword_div').style.display='block';
		//alert(response);
	}
}

////////////////////////Funciton to insert the keyword /////////////////////////////////
function handle_keywords_cloud() {
//	alert(document.getElementById("chk_keyword").length);
	
	 var chks  = document.getElementsByName('chk_keyword[]');
	 var key_str='';
	 
	 for(i=0;i<chks.length;i++)
     { 
	 	if(chks[i].checked) {
			key_str=key_str+chks[i].value+',';
	 	}
     } 
	 
	
	 
	 http.open('get', 'classes_mj/ajax_handler.php?action=handle_keywords&str=' 
			+ key_str);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleKeywordsCloud; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
	 
}
function handleKeywordsCloud(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		str_array=response.split('||=||');
	
		temp=document.getElementById('brand_meta_keywords').value;
		if(temp=='') {
			document.getElementById('brand_meta_keywords').innerHTML =str_array[0];
		}
		else {
			document.getElementById('brand_meta_keywords').innerHTML =trim(temp)+','+trim(str_array[0]);
		}
		
		document.getElementById('keyword_div').style.display='none';
		document.getElementById('metaHidden').value = str_array[1];
		
	}
}



////////////////////////////////////////////////////////////






function handle_keywords() {
//	alert(document.getElementById("chk_keyword").length);
	
	 var chks  = document.getElementsByName('chk_keyword[]');
	 var key_str='';
	 
	 for(i=0;i<chks.length;i++)
     { 
	 	if(chks[i].checked) {
			key_str=key_str+chks[i].value+',';
	 	}
     } 
	 
	
	 
	 http.open('get', 'classes_mj/ajax_handler.php?action=handle_keywords&str=' 
			+ key_str);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleKeywords; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
	 
}
function handleKeywords(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		
		str_array=response.split('||=||');
		document.getElementById('meta_tags').innerHTML = str_array[0];
		document.getElementById('keyword_div').style.display='none';
		document.getElementById('metaHidden').value = str_array[1];
		
	}
}

function hide_keywords_div() {
		document.getElementById('meta_tags').innerHTML = '';
		document.getElementById('keyword_div').style.display='none';
		document.getElementById('metaHidden').value = '';
}



function show_shipping_methods(owner,smID,partID) {
	
	document.getElementById('sm_div').innerHTML='<img src="images/loading3.gif" >';
	
	http.open('get', 'classes_mj/ajax_handler.php?action=fetch_shipping_options&ownerID='+owner+'&smID=' 
			+ smID+'&partID='+partID);
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleShipping; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

function handleShipping(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */
		var response = http.responseText;
		/* And now we want to change the product_categories <div> content.
			we do this using an ability to get/change the content of a page element 
			that we can find: innerHTML. */
		
		split_str=response.split("|@|@|");
		document.getElementById('moveforward').value=split_str[1];
		document.getElementById('sm_div').innerHTML=split_str[0];
		document.getElementById('radiocount').value=split_str[2];
		
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////
// Mayank Jaitly
// This is just for a testing and I know it is not going to work
// But atlast it is working :)
///////////////////////////////////////////////////////////////////////////////////////////////

function handlebb_post(PartID,ForumID,BrandID,UserID) {
	
    myString=window.location.href;
    mySplitResult=myString.split('/');
    var url = mySplitResult[0]+'//'+mySplitResult[2]+'/'+ 'modules.php?name=Forums';

    var parameters = 'file=posting1&sid=8a27857cd16d16e8de974cfcd32863fa&subject=This is for SEGA&addbbcode18=#444444&addbbcode20=12&helpbox=Underline text: [u]text[/u]  (alt+u)&message=Might It can be posted&attach_sig=on&notify=on&topictype=0&poll_title=&add_poll_option_text=&poll_length=&mode=newtopic&f='+ForumID+'&PartID='+PartID+'&post=Submit&wanted_new_perpage=100&wanted_a_perpage=100&actiontype=post&BrandID='+BrandID+'&UserID='+UserID;

    http.open("POST", url, true);
    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", parameters .length);
    http.setRequestHeader("Connection", "close");
    http.onreadystatechange = function() {//Handler function for call back on state change.
        if(http.readyState == 4) {
            var respText = http.responseText;
            if (respText.indexOf('messageForAjax:MESSAGE_POSTED') != -1) {
                alert('Forum Post Created.....');
            }
        }
    }
    http.send(parameters);
}

function handlebb_post_subbrand(PartID,ForumID,BrandID,UserID) {



	myString=window.location.href;
	mySplitResult=myString.split('/');
	var url = mySplitResult[0]+'//'+mySplitResult[2]+'/'+ 'modules.php?name=Forums';

var parameters = 'file=posting1&sid=8a27857cd16d16e8de974cfcd32863fa&subject=This is for SEGA&addbbcode18=#444444&addbbcode20=12&helpbox=Underline text: [u]text[/u]  (alt+u)&message=Might It can be posted&attach_sig=on&notify=on&topictype=0&poll_title=&add_poll_option_text=&poll_length=&mode=newtopic&f='+ForumID+'&PartID='+PartID+'&post=Submit&wanted_new_perpage=100&wanted_a_perpage=100&actiontype=post&BrandID='+BrandID+'&UserID='+UserID;

http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", parameters .length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Handler function for call back on state change.
    if(http.readyState == 4) {
        //alert(http.responseText);
        alert('Forum Post Created.....');
        var testvar='';
    }
}
http.send(parameters);
}


function handlebb_post_reply(PartID,TopicID) {

myString=window.location.href;
mySplitResult=myString.split('/');
var url = mySplitResult[0]+'//'+mySplitResult[2]+'/'+ 'modules.php?name=Forums';
var parameters = 'file=posting1&sid=8a27857cd16d16e8de974cfcd32863fa&subject=This is for SEGA&addbbcode18=#444444&addbbcode20=12&helpbox=Underline text: [u]text[/u]  (alt+u)&message=Might It can be posted&attach_sig=on&notify=on&topictype=0&poll_title=&add_poll_option_text=&poll_length=&mode=reply&t='+TopicID+'&PartID='+PartID+'&post=Submit&wanted_new_perpage=100&wanted_a_perpage=100&actiontype=reply';

http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", parameters .length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Handler function for call back on state change.
    if(http.readyState == 4) {
        //alert(http.responseText);
        alert('Part successfully offered..');
        var testvar='';
    }
}
http.send(parameters);
}




